package com.training.schedule_management_system_admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.training.schedule_management_system_admin.constant.Msg;
import com.training.schedule_management_system_admin.entity.Scheduletable;
import com.training.schedule_management_system_admin.entity.Scheduletemplate;
import com.training.schedule_management_system_admin.service.ScheduletemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/scheduletemplate")
public class ScheduletemplateController {

    @Autowired
    ScheduletemplateService scheduletemplateService;

    /*
    来到日程模版页面
     */
    @RequestMapping("/toTemplate")
    public String toTemplate(@RequestParam(value = "pageNum",defaultValue ="1") int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                             @RequestParam(value = "navigatePages",defaultValue = "5") int navigatePages,
                             Model model){
        JSONObject jsonObject=new JSONObject();
        PageHelper.startPage(pageNum, pageSize);
        List<Scheduletemplate> templates=scheduletemplateService.getAllWithUser();
        PageInfo page=new PageInfo(templates,navigatePages);
        jsonObject.put("page",page);
        model.addAttribute("template",new Scheduletemplate());
        model.addAttribute("msg",jsonObject);
        return "/user/scheduletemplate";
    }

    /*
    创建日程模版
   */
    @PostMapping("/createTemplate")
    public String createTemplate(@ModelAttribute("template")Scheduletemplate template, Model model){
        JSONObject jsonObject=new JSONObject();
        scheduletemplateService.create(template);
        jsonObject.put("msg","创建成功");
        model.addAttribute("result",jsonObject);
        return "/user/result";
    }

    /*
    得到某个日程模版
     */
    @ResponseBody
    @RequestMapping("/getTemplate/{templateId}")
    public Msg getTemplate(@PathVariable("templateId") Integer templateId){
        Scheduletemplate template=scheduletemplateService.getById(templateId);
        if(template==null){
            return Msg.fail();
        }
        return Msg.success().add("template",template);
    }

    /*
    得到当前用户的所有日程模版
     */
    @ResponseBody
    @RequestMapping("/getTemplates")
    public Msg getTemplatesByUserId(){
        List<Scheduletemplate> templates = scheduletemplateService.getAllWithUser();
        return Msg.success().add("templates",templates);
    }

    /*
    修改日程模版
     */
    @PostMapping("/updateTemplate")
    public String updateTemplate(@ModelAttribute("template")Scheduletemplate template,Model model){
        JSONObject jsonObject=new JSONObject();
        Scheduletemplate scheduletemplate=scheduletemplateService.getById(template.getId());
        if(scheduletemplate==null){
            jsonObject.put("msg","该日程模版不存在!");
            model.addAttribute("result",jsonObject);
            return "/user/result";
        }
        scheduletemplateService.update(template);
        jsonObject.put("msg","修改成功!");
        model.addAttribute("result",jsonObject);
        return "/user/result";
    }
}
